using System;
using System.Collections.Generic;
using System.Numerics;
using TTEngine.Graphics;
using TTEngine.Resource;
using TTEngine.TTMath;

namespace TTEngine.Framework.UI
{
    public class UICanvas : UIBaseContainer
    {

        public Batcher Batcher
        {
            get;
            private set;
        }
        public ITarget Target
        {
            get;
            private set;
        }
        public Camera _camera
        {
            get;
            private set;
        }
        private List<Action> _events = new List<Action>();

        public UICanvas(ITarget target, CommonResource resource)
        {
            Target = target;
            _camera = new Camera();
            Batcher = new Batcher(resource);
        }
        public void SetTarget(ITarget target)
        {
            this.Target = target;
        }
        public void FillTarget()
        {
            if (Target == null)
                return;

            Vector2 lookat;
            lookat.X = Target.Width / 2 / _camera.Scale;
            lookat.Y = Target.Height / 2 / _camera.Scale;
            _camera.LookAt = lookat;

            float scaleWidth = Target.Width / _camera.Scale;
            float scaleHeight = Target.Height / _camera.Scale;
            LocalRect.SetByRect(new Rectangle(0, 0, scaleWidth, scaleHeight));
        }


        public override void OnUpdate(UICanvas canvas, float delta)
        {
            if (!Enable)
                return;

            if (Target != null && _camera != null)
            {
                FillTarget();
            }
            else
            {
                return;
            }

            base.OnUpdate(this, delta);

            if (_events.Count > 0)
            {
                foreach (var evt in _events)
                {
                    evt();
                }
                _events.Clear();
            }
        }


        public override void OnRender(UICanvas canvas)
        {
            if (!Enable)
                return;

            if (_camera == null || Target == null)
                return;

            FillTarget();
            _camera.Apply();
            Batcher.BeginDraw();
            base.OnRender(this);
            Batcher.EndDraw();

        }

        public override bool OnTouch(UICanvas canvas, int touchId, bool press, bool move, float x, float y)
        {
            if (_camera == null)
                return false;

            var scale = 1.0f;
            if (Target is IView v)
            {
                scale = v.Scale;
            }
            float fs = scale / _camera.Scale;
            float tx = x * fs;
            float ty = y * fs;
            return base.OnTouch(this, touchId, press, move, tx, ty);
        }

        public override bool OnWheel(UICanvas canvas, float dx, float dy, float dz)
        {
            return base.OnWheel(this, dx, dy, dz);
        }

        public void InvokeEvent(Action evt)
        {
            _events.Add(evt);
        }
    }
}